package cn.fl.riskctrl.ccard.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.riskctrl.ccard.service.ICcardV2ResultService;
import cn.fl.riskctrl.vo.RcCcardv2CstResultVO;
import cn.fl.riskctrl.vo.RcCcardv2PrjResultVO;
import cn.fl.riskctrl.vo.RcCcardv2RiskResultVO;

import cn.fl.risknew.service.IRiskLevelService;
import cn.fl.risknew.vo.CCardResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 百融-征信记录
 * 控制层
 */
@Controller
@RequestMapping("/riskctrl/ccardv2")
public class CcardV2CstInfoController extends BaseController{

	@Autowired
	private ICcardV2ResultService ccardV2ResultService;

	@Resource
	private IRiskLevelService riskLevelService;

	/**
	 * 跳转C卡客户列表
	 */
	@RequestMapping(value = "/toCcardV2CstInfoPage.do", method = { RequestMethod.GET })
	public ModelAndView toCcardCstInfoPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.newLogger("跳转C卡客户列表页面，请求参数：{}", this.getRequestParameter(request)).info();

        ModelAndView view = new ModelAndView("riskctrl/ccard/ccardV2CstInfoList");
        // 客户ID
		String cstId = request.getParameter("cstId");
		view.addObject("cstId", cstId);

		return view;
	}
	@RequestMapping(value = "/searchCcardCstListPage.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object searchCcardCstListPage(HttpServletRequest request, HttpServletResponse response)  {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.newLogger("获取C卡客户结果列表.请求参数：{}", this.getRequestParameter(request)).info();


        /*ListPageVO<RcCcardv2CstResultVO> listPage = new ListPageVO<RcCcardv2CstResultVO>();*/
		/*try {
            *//*Pager page = new Pager();*//*
            String cstId = request.getParameter("cstId");
            if(StringUtils.isEmpty(cstId)) {
                BaseLogUtils.newLogger("获取C卡客户结果列表失败：cstId不可为空!").error(LogExceptionEnum.COMMON_CODE_0001);
                return null;
            }
*//*
            String currentPage = request.getParameter("page");
            String pageSize = request.getParameter("pagesize");

            if (StringUtils.isNotEmpty(currentPage)) {
                page.setCurrentPage(Integer.parseInt(currentPage));
            }
            if (StringUtils.isNotEmpty(pageSize)) {
                page.setEveryPage(Integer.parseInt(pageSize));
            }*//*

            // 获取数据
            *//*Pager pager = this.ccardV2ResultService.getCcardV2CstResultList(page, Long.valueOf(cstId));

            List<RcCcardv2CstResultVO> list = (List<RcCcardv2CstResultVO>) pager.getResultList();
            listPage = new ListPageVO<RcCcardv2CstResultVO>(list, pager.getTotalCount());*//*

            return null;

		} catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡客户结果列表失败:{}", e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
		} finally {
            Long endTm = System.currentTimeMillis();
            BaseLogUtils.newLogger( "获取C卡客户结果列表结束，耗时：{}", (endTm-startTm)).info();
        }*/
        String cstId = request.getParameter("cstId");
        if(StringUtils.isEmpty(cstId)) {
            BaseLogUtils.newLogger("获取C卡客户结果列表失败：cstId不可为空!").error(LogExceptionEnum.COMMON_CODE_0002);
            return null;
        }
        try {
           return   riskLevelService.getCRiskLevel(Long.parseLong(cstId));
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡客户结果列表失败：cstId不可为空!").error(LogExceptionEnum.COMMON_CODE_0001);
        }
        return null;
	}

    /**
     * 跳转：C卡项目列表
     */
    @RequestMapping(value = "/toCcardV2PrjInfoPage.do", method = { RequestMethod.GET })
    public ModelAndView toCcardPrjInfoPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.newLogger(" 到列表页面.请求参数：{}", this.getRequestParameter(request)).info();

        ModelAndView view = new ModelAndView("riskctrl/ccard/ccardV2PrjInfoList");
        // 客户ID
        String cstId = request.getParameter("cstId");
        // 批次号
        String batchNo = request.getParameter("batchNo");
        view.addObject("cstId", cstId);
        view.addObject("batchNo", batchNo);

        return view;
    }
    @RequestMapping(value = "/searchCcardPrjListPage.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object searchCcardPrjListPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.newLogger("获取C卡项目结果列表.请求参数：{}", this.getRequestParameter(request)).info();

        ListPageVO<RcCcardv2PrjResultVO> listPage = new ListPageVO<RcCcardv2PrjResultVO>();
        try {
            String cstId = request.getParameter("cstId");
            String batchNo = request.getParameter("batchNo");
            if(StringUtils.isEmpty(cstId) || StringUtils.isEmpty(batchNo)) {
                BaseLogUtils.newLogger("获取C卡项目结果列表失败：cstId和batchNo不可为空!").error(LogExceptionEnum.COMMON_CODE_0001);
                return listPage;
            }

            String currentPage = request.getParameter("page");
            String pageSize = request.getParameter("pagesize");
            Pager page = new Pager();
            if (StringUtils.isNotEmpty(currentPage)) {
                page.setCurrentPage(Integer.parseInt(currentPage));
            }
            if (StringUtils.isNotEmpty(pageSize)) {
                page.setEveryPage(Integer.parseInt(pageSize));
            }

            // 获取数据
            Pager pager = this.ccardV2ResultService.getCcardV2PrjResultList(page, Long.valueOf(cstId), batchNo);

            List<RcCcardv2PrjResultVO> list = (List<RcCcardv2PrjResultVO>) pager.getResultList();
            listPage = new ListPageVO<RcCcardv2PrjResultVO>(list, pager.getTotalCount());
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡项目结果列表失败:{}", e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw new BaseException("获取C卡项目结果列表失败！");
        } finally {
            Long endTm = System.currentTimeMillis();
            BaseLogUtils.newLogger( "获取C卡项目结果列表结束，耗时：{}", (endTm-startTm)).info();
        }

        return listPage;
    }

    /**
     * 跳转到客户的三方+矢量列表的页面
     */
    @RequestMapping(value = "/toCcardV2RiskInfoPage.do", method = { RequestMethod.GET })
    public ModelAndView toCcardV2RiskInfoPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.newLogger("跳转到客户的三方+矢量列表的页面：{}", this.getRequestParameter(request)).info();

        ModelAndView view = new ModelAndView("riskctrl/ccard/ccardV2RiskInfoList");
        // 客户ID
        String dataId = request.getParameter("dataId");
        view.addObject("dataId", dataId);

        List<String[]> cjwsValues = this.ccardV2ResultService.getCcardV2BrcjwsRiskResultList(Long.valueOf(dataId));
        view.addObject("cjwsValue", cjwsValues);

        Long endTm = System.currentTimeMillis();
        BaseLogUtils.newLogger("跳转到客户的三方+矢量列表的页面.耗时：" + (endTm-startTm)).info();
        return view;
    }

    /**
     * C卡客户的三方+业务矢量
     */
    @RequestMapping(value = "/searchCcardRiskListPage.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object searchCcardRiskListPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.newLogger("获取C卡三方结果列表.请求参数：{}", this.getRequestParameter(request)).info();

        ListPageVO<RcCcardv2RiskResultVO> listPage = new ListPageVO<RcCcardv2RiskResultVO>();
        try {
            String currentPage = request.getParameter("page");
            String pageSize = request.getParameter("pagesize");
            String dataId = request.getParameter("dataId");
            if(StringUtils.isEmpty(dataId)) {
                return listPage;
            }

            Pager page = new Pager();
            if (StringUtils.isNotEmpty(currentPage)) {
                page.setCurrentPage(Integer.parseInt(currentPage));
            }
            if (StringUtils.isNotEmpty(pageSize)) {
                page.setEveryPage(Integer.parseInt(pageSize));
            }

            Pager pager = this.ccardV2ResultService.getCcardV2RiskResultList(page, Long.valueOf(dataId));
            List<RcCcardv2RiskResultVO> list = (List<RcCcardv2RiskResultVO>) pager.getResultList();
            listPage = new ListPageVO<RcCcardv2RiskResultVO>(list, pager.getTotalCount());
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡三方结果列表失败:{}", e).error(LogExceptionEnum.COMMON_CODE_0002, e);
        } finally {
            Long endTm = System.currentTimeMillis();
            BaseLogUtils.newLogger( "获取C卡三方结果列表结束，耗时：{}", (endTm-startTm)).info();
        }

        return listPage;
    }

}
